package com.qf.Dao.impl;

import com.qf.Dao.MedicineInfoDao;
import com.qf.Utils.DBUtils;
import com.qf.entity.MedicineInfo;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class MedicineInfoDaoImpl implements MedicineInfoDao {

    private QueryRunner qr = new QueryRunner(DBUtils.DB.getDs());

    @Override
    public int addMedicineInfo(MedicineInfo medicineInfo) throws SQLException {

        String sql = "INSERT INTO medicine_Info(id,medicine_name,medicine_price,medicine_value)VALUES(?,?,?,?)";
        Object[] objects = {medicineInfo.getId(), medicineInfo.getMedicine_Name(), medicineInfo.getMedicine_Price(), medicineInfo.getMedicine_Value()};
        return qr.update(sql, objects);
    }

    @Override
    public int updateMedicineInfo(MedicineInfo medicineInfo) throws SQLException {
        String sql = "UPDATE medicine_info SET medicine_name=?,medicine_price=?,medicine_value WHERE id=?";
        Object[] objects = {medicineInfo.getMedicine_Name(), medicineInfo.getMedicine_Price(), medicineInfo.getMedicine_Value(), medicineInfo.getId()};
        return qr.update(sql, objects);
    }

    @Override
    public int deleteMedicineInfo(int id) throws SQLException {
        String sql = "DELETE  FROM  medicine_info WHERE id=? ";
        return qr.update(sql);
    }

    @Override
    public MedicineInfo selectByMedicine_name(String medicine_name) throws SQLException {
        String sql = "SELECT * FROM medicine_info WHERE medicine_name =?";
        MedicineInfo medicineInfo = qr.query(sql, new BeanHandler<MedicineInfo>(MedicineInfo.class), medicine_name);
        return medicineInfo;
    }
}
